﻿#region Header
// ------------------------ Licence / Copyright ------------------------
// 
// ScrumTable for Scrum Meeting Support
// Copyright © HSR - Hochschule für Technik Rapperswil
// All Rights Reserved
// 
// Author:
//  Michael Gfeller Silvan Gehrig Patrick Boos
// 
// ---------------------------------------------------------------------
#endregion

#region Usings

using System;
using System.Globalization;
using System.Text.RegularExpressions;
using System.Windows;
using System.Windows.Data;

#endregion

namespace ScrumTable.UI.View.Converter
{

    /// <summary>
    /// Removes all tags from a html string
    /// </summary>
    [ValueConversion(typeof(string), typeof(string))]
    public class ConverterRemoveHtmlTags : IValueConverter
    {

        public int? MaxCharCount { get; set; }

        public bool IsHtml { get; set; }
       

        public ConverterRemoveHtmlTags()
        {
            IsHtml = true;
        }

        #region Methods
        //--------------------------------------------------------------------
        // Methods
        //--------------------------------------------------------------------
        /// <summary>
        /// string without html tags;
        /// </summary>
        /// <param name="value">The value produced by the binding source.</param>
        /// <param name="targetType">The type of the binding target property.</param>
        /// <param name="parameter">The converter parameter to use.</param>
        /// <param name="culture">The culture to use in the converter.</param>
        /// <returns>
        /// string without html tags;
        /// </returns>
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if(value==null)
            {
                return "";
            }
            var noHtml = IsHtml ? StripTagsCharArray(value.ToString()) : value.ToString();
            if((MaxCharCount.HasValue && MaxCharCount.Value < noHtml.Length))
            {
                return noHtml.Substring(0, MaxCharCount.Value ) + "... ";
            }
            return noHtml;
        }

        /// <summary>
        /// Not supported. Throws NotSupportedException.
        /// </summary>
        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            throw new NotSupportedException();

        }
        
        //faster as  regex
        public string StripTagsCharArray(string source)
        {
            char[] array = new char[source.Length];
            int arrayIndex = 0;
            bool inside = false;
            bool insideAnd = false;

            for (int i = 0; i < source.Length; i++)
            {
                char let = source[i];
                if (let == '<')
                {
                    inside = true;
                    continue;
                }
                if(let == '&')
                {
                    insideAnd = true;
                    continue;
                }
                if (let == ';')
                {
                    insideAnd = false;
                    continue;
                }
                if (let == '>' || let ==';')
                {
                    inside = false;
                    continue;
                }
                if (!inside && !insideAnd)
                {
                    array[arrayIndex] = let;
                    arrayIndex++;
                }
            }
            return new string(array, 0, arrayIndex);
        }

        /// <summary>
        /// Compiled regular expression for performance.
        /// </summary>
        static Regex _htmlRegex = new Regex("<.*?>", RegexOptions.Compiled);

        /// <summary>
        /// Remove HTML from string with compiled Regex.
        /// </summary>
        public static string StripTagsRegexCompiled(string source)
        {
            return _htmlRegex.Replace(source, string.Empty);
        }

       
        #endregion
       
    }
}
